<template>
   <div>
    <el-table
      :data="tableData"
      border
    >
      <el-table-column type="index" label="#" width="50" />
      <el-table-column prop="userName" label="昵称" width="150" show-overflow-tooltip />
      <el-table-column prop="date" label="头像" width="180">
        <template #default="scope">
          <img class="header-img" :src="scope.row.headImg" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="motto" label="座右铭" width="180" show-overflow-tooltip />
      <el-table-column label="作业地址">
        <template #default="scope">
          <a :href="scope.row.workUrl" target="_blank" >{{ scope.row.workUrl }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="ip" label="ip地址" />
      <el-table-column  label="状态" >
        <template #default="scope">
           <el-tag v-if="userIsLineIds.includes(scope.row.userId)" type="primary">在线</el-tag>
           <el-tag v-else type="info">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column  label="操作" >
        <template #default="scope">
          <el-button type="text" @click="sendMsg(scope.row)">呼叫</el-button>
          <el-button type="text" @click="showDialog(scope.row)">对话</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pageBox :total="totalNum" :pageData="pageData" @pageSizeChange="pageSizeChange" @pageChange="pageChange" />
    <Dialog ref="dialogRef" />
   </div>
</template>

<script lang="ts">
import { getPageData } from '@/api/userInfo'
import pageBox from '@/components/pageBox.vue'
import { userStatusList } from '@/socket/'
import { ElMessageBox, ElNotification } from 'element-plus'
import { sendMessageByUserId } from '@/api/talk'
import Dialog from './dialogTalk/index.vue'
export default {
  components: {
    pageBox,
    Dialog
  },
  data() {
    return {
      userStatusList,
      tableData: [],
      totalNum: 0,
      pageData: {
        page: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    userIsLineIds() {
      return this.userStatusList.map(item => item.userId);
    }
  },

  created() {
    this.getData()
  },
  methods: {
    sendMsg(row: any) {
      ElMessageBox.prompt('请输入一条您要发送的消息', `向${row.userName}发送消息`, {
        confirmButtonText: '发送',
        cancelButtonText: '取消',
        inputPattern: /^\S+$/,
        inputErrorMessage: '请输入内容'
      }).then(({ value }) => {
        sendMessageByUserId({
          targetUserId: row.userId,
          type: 1,
          message: value
        }).then(res => {
          ElNotification.success('发送成功！')
        })
      })
    },
    getData() {
      // 接口数据
      getPageData(this.pageData).then(res => {
        this.tableData = res.data.rows;
        this.totalNum = res.data.total
      })
    },
    pageSizeChange(pageSize: number) {
      this.pageData.pageSize = pageSize
      this.pageData.page = 1;
      this.getData() // 请求数据
    },
    pageChange(page: number) {
      this.pageData.page = page
       this.getData() // 请求数据
    },
    showDialog(row: any) {
      const refs = this.$refs.dialogRef as any
      refs.open(row)
    }
  }
}
</script>

<style lang="scss" scoped>
.header-img{
  width: 50px;
  height: 50px;
}


</style>
